package com.doge.walknovel.business.home.genres

import com.doge.walknovel.api.model.NovelInfo
import com.doge.walknovel.utils.*
import java.io.*

object GenresCacheHelper {
    private val novelsDir: File get() {
        return File(appContext.externalCacheDir, "genres").apply {
            if (!exists()) mkdirs()
        }
    }

    fun saveCache(key: String, data: List<NovelInfo>) = runOnIO {
        val file = File(novelsDir, key)
        FileUtils.write(file, toJson(data))
    }

    fun loadCache(key: String): String? {
        require(!isMainThread())
        val file = File(novelsDir, key)
        return FileUtils.read(file)
    }
}